package tinglema.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import tinglema.server.common.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import tinglema.server.entity.domain.RankList;
import tinglema.server.service.RankListService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 歌单评分(RankList)表控制层
 *
 * @author delucia
 * @since 2025-05-06 02:53:06
 */
@RestController
@RequestMapping("rankList")
public class RankListController {
    /**
     * 服务对象
     */
    @Resource
    private RankListService rankListService;


    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param rankList 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<RankList> page, RankList rankList) {
        return R.success(this.rankListService.page(page, new QueryWrapper<>(rankList)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.success(this.rankListService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param rankList 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody RankList rankList) {
        return R.success(this.rankListService.save(rankList));
    }

    /**
     * 修改数据
     *
     * @param rankList 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody RankList rankList) {
        return R.success(this.rankListService.updateById(rankList));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.rankListService.removeByIds(idList));
    }

    /* 客户端API*/


    /**
     * 根据歌单ID获取歌单的评分排名
     * 此方法通过歌单ID调用服务层方法，计算该歌单的总评分，从而间接反映歌单的排名情况
     *
     * @param songListId 歌单ID，通过URL路径变量获取
     * @return 返回一个结果对象，包含歌单的总评分信息
     */
    @GetMapping("songListId/{songListId}")
    public R rankOfSongListId(@PathVariable Long songListId) {
        return R.success(null, rankListService.selectScoreSum(songListId));
    }


    //   // 获取指定用户对指定歌单的评分
    //   getUserRank: (consumerId, songListId) =>
    //     get(`rankList?songListId=${songListId}&consumerId=${consumerId}`),
    @GetMapping("/userRank")
    public R getUserRank(@RequestParam Long consumerId, @RequestParam Long songListId) {
        LambdaQueryWrapper<RankList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RankList::getConsumerId, consumerId)
                .eq(RankList::getSongListId, songListId);
        RankList rankList = rankListService.getOne(queryWrapper);
        if (rankList == null) {
            return R.error("无评分记录");
        }
        return R.success(null, rankList.getScore());
    }

    //add
    @PostMapping("/add")
    public R add(@RequestBody RankList rankList) {
        return R.success(rankListService.save(rankList));
    }
}

